package com.gowhere.service.impl.basedata;

import com.github.pagehelper.PageHelper;
import com.gowhere.domain.City;
import com.gowhere.domain.Product;
import com.gowhere.mapper.basedata.ProductMapper;
import com.gowhere.service.basedata.ProductService;
import com.gowhere.utils.MapperUtil;
import com.gowhere.utils.UidUtil;
import org.apache.ibatis.session.SqlSession;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    @Override
    public List<Product> findAll(int pageNum, int pageSize) {
        // 业务层进行持久化操作
        // 1. 工具类获取session
        SqlSession session = MapperUtil.getSession();
        // 2. 获取代理对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        // 设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        // 3. 代理对象使用方法进行操作
        List<Product> list = mapper.findAll();
        // 增强for进行遍历
        for (Product product : list) {
            // 创建格式化时间类对象
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss") ;
            // product集合中遍历中获取的对象得到的时间对象,进行格式化成如上格式的string类型数据,这个数据就是dts
            String departureTimeStr = simpleDateFormat.format(product.getDepartureTime());
            //  设置这个dts成要返回的DTS数据
            product.setDepartureTimeStr(departureTimeStr);
            // 状态值 = 循环中获取的此product对象的值变成string
            String productStatusStr = String.valueOf(product.getProductStatus());
            // 设置成要返回的PTS数据
            product.setProductStatusStr(productStatusStr);

        }
        // 4. 释放资源
        MapperUtil.close(session);
        System.out.println(list);
        return list;
    }

    @Override
    public void save(Product product) {
        // 创建唯一字符转数据用于id,使用UidUnit的getUid方法
        String uid = UidUtil.getUid();
        // 已经封装的product对象使用添加id的方法setId(),参数就是这个uid
        product.setId(uid);
        // 数据库需要的数据应该已经完整了,现在进行持久化操作
        // 1. 获取session
        SqlSession session = MapperUtil.getSession();
        // 2. 获取代理对象
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        Date departureTime = product.getDepartureTime();
        System.out.println(departureTime+" 121212");
        // 3. 执行操作
        mapper.save(product);
        // 4 .释放资源
        MapperUtil.close(session);
    }

    @Override
    public List<City> findAllCity() {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        List<City> list = mapper.findAllCity();
        MapperUtil.close(session);
        return list;
    }

    @Override
    public Product findById(String id) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        Product product = mapper.findById(id);

        /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm") ;
        String departureTimeStr = simpleDateFormat.format(product.getDepartureTime());
        product.setDepartureTimeStr(departureTimeStr)  ;
        String productStatusStr = String.valueOf(product.getProductStatus());
        product.setProductStatusStr(productStatusStr);*/

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        product.setDepartureTimeStr(sf.format(product.getDepartureTime()));

        MapperUtil.close(session);
        return product;
    }

    @Override
    public void update(Product product) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm" );
        /*String departureTimeStr = product.getDepartureTimeStr();
        try {
            Date parse = s.parse(departureTimeStr);
            product.setDepartureTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = Integer.parseInt(product.getProductStatusStr());
        product.setProductStatus(i);*/

        product.setDepartureTimeStr(s.format(product.getDepartureTime()));
        mapper.update(product);
        MapperUtil.close(session);
    }

    @Override
    public void deleteById(String id) {
        SqlSession session = MapperUtil.getSession();
        ProductMapper mapper = session.getMapper(ProductMapper.class);
        mapper.deleteById(id);
        MapperUtil.close(session);
    }
}
